<template>
  <div class="searchedArticleBox">
    <div class="top">
      <div class="left">
        <van-dropdown-menu>
          <van-dropdown-item
            v-model="value1"
            :options="option1"
            get-container="body"
          />
        </van-dropdown-menu>
      </div>
      <div class="right">
        <div class="isDown" @click.stop="changeIsDown">全部</div>
        <div class="isDown" @click.stop="changeIsDown">可下载</div>
        <div class="isDownLid" :class="{ all: !isDown, only: isDown }">
          {{ isDown ? "可下载" : "全部" }}
        </div>
      </div>
    </div>
    <div class="index">
      <div class="total">
        共
        <span>{{ total }}</span>
        部作品
      </div>
      <div class="searchedArticles" v-if="searchedArticles">
        <SimpleVideo
          v-for="searchedArticle in searchedArticles"
          :key="searchedArticle.id"
          :videoInfo="searchedArticle"
        />
      </div>
    </div>
  </div>
</template>

<script>
import SimpleVideo from "@/components/SimpleVideo.vue";

export default {
  components: {
    SimpleVideo,
  },
  data() {
    return {
      sort: "hot",
      allow_download: 0,
      value1: 0,
      option1: [
        { text: "最热门", value: 0 },
        { text: "最匹配", value: 1 },
        { text: "新发布", value: 2 },
      ],
      isDown: false,
      total: 0,
      searchedArticles: null,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      this.$axios
        .get(
          `https://apis.netstart.cn/xpc/search?type=article&kw=${this.$route.query.kw}&sort=${this.sort}&page=&allow_download=${this.allow_download}`
        )
        .then((res) => {
          // console.log(res.data.data);
          this.total = res.data.data.total;
          this.searchedArticles = res.data.data.list;
          this.$toast.clear();
        });
    },
    changeIsDown() {
      this.isDown = !this.isDown;
    },
  },
  watch: {
    value1(newVal) {
      // console.log(newVal);
      if (newVal == 0) {
        this.sort = "hot";
      } else if (newVal == 1) {
        this.sort = "default";
      } else if (newVal == 2) {
        this.sort = "addtime";
      }
      this.getData();
    },
    isDown(newVal) {
      if (!newVal) {
        this.allow_download = 0;
      } else {
        this.allow_download = 1;
      }
      this.getData();
    },
  },
};
</script>

<style lang="scss" scoped>
.searchedArticleBox {
  padding-top: 85px;
  height: 510px;
  overflow-y: scroll;

  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    // padding: 0 15px;
    border-bottom: 1px solid #f2f2f2;
    position: fixed;
    left: 0;
    top: 34px;
    z-index: 10;
    width: 100%;
    height: 50px;
    background-color: white;

    .left {
      margin-left: 7px;

      :deep(.van-dropdown-menu__bar) {
        box-shadow: none;
        background-color: transparent;
      }

      :deep(.van-dropdown-menu__title) {
        font-weight: bold;
      }
    }

    .right {
      margin-right: 15px;
      width: 100px;
      height: 30px;
      border-radius: 15px;
      background-color: #f0f0f0;
      display: flex;
      position: relative;
      font-size: 14px;
      padding: 0 5px;

      .isDown {
        color: #999;
        width: 50%;
        height: 30px;
        line-height: 30px;
        text-align: center;
      }

      .isDownLid {
        position: absolute;
        left: 1px;
        top: 1px;
        width: 50px;
        height: 28px;
        line-height: 28px;
        text-align: center;
        border-radius: 14px;
        background-color: white;
        font-size: 13px;
      }

      .all {
        left: 1px;
        transition: left 0.5s;
      }

      .only {
        left: 58px;
        transition: left 0.5s;
      }
    }
  }

  .index {
    margin-top: 15px;
    padding: 0 15px;

    .total {
      font-size: 13px;
      color: #999;

      span {
        color: red;
        font-weight: bold;
      }
    }
  }
}
</style>
